{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "color-kuwait",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Setting seed for reproducability\n",
    "np.random.seed(1234)  \n",
    "PYTHONHASHSEED = 0\n",
    "from sklearn import preprocessing\n",
    "from sklearn.metrics import confusion_matrix, recall_score, precision_score\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, LSTM, Activation\n",
    "%matplotlib inline\n",
    "\n",
    "def load():\n",
    "    train_df = pd.read_csv('PM_train.txt', sep=\" \", header=None)\n",
    "    train_df.drop(train_df.columns[[26, 27]], axis=1, inplace=True)\n",
    "    train_df.columns = ['id', 'cycle', 'setting1', 'setting2', 'setting3', 's1', 's2', 's3',\n",
    "                     's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11', 's12', 's13', 's14',\n",
    "                     's15', 's16', 's17', 's18', 's19', 's20', 's21']\n",
    "    train_df = train_df.sort_values(['id','cycle'])\n",
    "\n",
    "    # Data Labeling - generate column RUL\n",
    "    rul = pd.DataFrame(train_df.groupby('id')['cycle'].max()).reset_index()\n",
    "    rul.columns = ['id', 'max']\n",
    "    train_df = train_df.merge(rul, on=['id'], how='left')\n",
    "    train_df['RUL'] = train_df['max'] - train_df['cycle']\n",
    "    train_df.drop('max', axis=1, inplace=True)\n",
    "    train_df.head()\n",
    "    \n",
    "    # generate label columns for training data\n",
    "    w1 = 30\n",
    "    w0 = 15\n",
    "    train_df['label1'] = np.where(train_df['RUL'] <= w1, 1, 0 )\n",
    "    train_df['label2'] = train_df['label1']\n",
    "    train_df.loc[train_df['RUL'] <= w0, 'label2'] = 2\n",
    "    train_df['cycle_norm'] = train_df['cycle']\n",
    "    cols_normalize = train_df.columns.difference(['id','cycle','RUL','label1','label2'])\n",
    "    \n",
    "    min_max_scaler = preprocessing.MinMaxScaler()\n",
    "    norm_train_df = pd.DataFrame(min_max_scaler.fit_transform(train_df[cols_normalize]), \n",
    "                                 columns=cols_normalize, \n",
    "                                 index=train_df.index)\n",
    "    join_df = train_df[train_df.columns.difference(cols_normalize)].join(norm_train_df)\n",
    "    train_df = join_df.reindex(columns = train_df.columns)\n",
    "    train_df\n",
    "    \n",
    "    #按照同样的思路，对测试集和真实集先正则化，后降维\n",
    "    # read test data\n",
    "    test_df = pd.read_csv('./PM_test.txt', sep=\" \", header=None)\n",
    "    test_df.head()\n",
    "    test_df.drop(test_df.columns[[26, 27]], axis=1, inplace=True)\n",
    "    test_df.columns = ['id', 'cycle', 'setting1', 'setting2', 'setting3', 's1', 's2', 's3',\n",
    "                         's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11', 's12', 's13', 's14',\n",
    "                         's15', 's16', 's17', 's18', 's19', 's20', 's21']\n",
    "    test_df['cycle_norm'] = test_df['cycle']\n",
    "    test_df.head()\n",
    "    norm_test_df = pd.DataFrame(min_max_scaler.transform(test_df[cols_normalize]), \n",
    "                                columns=cols_normalize, \n",
    "                                index=test_df.index)\n",
    "    # read ground truth data\n",
    "    truth_df = pd.read_csv('PM_truth.txt', sep=\" \", header=None)\n",
    "    truth_df.drop(truth_df.columns[[1]], axis=1, inplace=True)\n",
    "    train_df = train_df.sort_values(['id','cycle'])\n",
    "    test_df['cycle_norm'] = test_df['cycle']\n",
    "\n",
    "    norm_test_df = pd.DataFrame(min_max_scaler.transform(test_df[cols_normalize]), \n",
    "                                columns=cols_normalize, \n",
    "                                index=test_df.index)\n",
    "\n",
    "    test_join_df = test_df[test_df.columns.difference(cols_normalize)].join(norm_test_df)\n",
    "    test_df = test_join_df.reindex(columns = test_df.columns)\n",
    "    test_df = test_df.reset_index(drop=True)\n",
    "    # read ground truth data\n",
    "    truth_df = pd.read_csv('PM_truth.txt', sep=\" \", header=None)\n",
    "    truth_df.drop(truth_df.columns[[1]], axis=1, inplace=True)\n",
    "    # generate column max for test data\n",
    "    rul = pd.DataFrame(test_df.groupby('id')['cycle'].max()).reset_index()\n",
    "\n",
    "    rul.columns = ['id', 'max']\n",
    "    truth_df.columns = ['more']\n",
    "    truth_df['id'] = truth_df.index + 1\n",
    "    truth_df['max'] = rul['max'] + truth_df['more']\n",
    "\n",
    "    truth_df.drop('more', axis=1, inplace=True)\n",
    "\n",
    "    # generate RUL for test data\n",
    "    test_df = test_df.merge(truth_df, on=['id'], how='left')\n",
    "    test_df['RUL'] = test_df['max'] - test_df['cycle']\n",
    "    test_df.drop('max', axis=1, inplace=True)\n",
    "\n",
    "    # generate label columns w0 and w1 for test data\n",
    "    test_df['label1'] = np.where(test_df['RUL'] <= w1, 1, 0 )\n",
    "    test_df['label2'] = test_df['label1']\n",
    "    test_df.loc[test_df['RUL'] <= w0, 'label2'] = 2\n",
    "    test_df.head()\n",
    "    \n",
    "    return train_df,test_df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
